/**
 * @filename:SupplierContractDetailController 2022-03-22 02:40:49
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2020 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blue.commont.base.*;
import com.lwl.blue.commont.utils.UserContextUtil;
import com.lwl.blue.entity.SupplierContractDetail;
import com.lwl.blue.service.FilesService;
import com.lwl.blue.service.SupplierContractDetailService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Objects;

/**
 * <p>自定义方法写在这里</p>
 *
 * <p>说明： API接口层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
@Api(tags = "")
@RestController
@RequestMapping("/supplierContractDetail")
@Slf4j
public class SupplierContractDetailController extends BaseController {
    @Autowired
    SupplierContractDetailService supplierContractDetailService;
    @Resource
    FilesService filesService;

    @PostMapping("/pages")
    public PageResult<SupplierContractDetail> getPages(@RequestBody PageParam<SupplierContractDetail> pageParam) {
        JsonResult<Page<SupplierContractDetail>> pages = supplierContractDetailService.pages(pageParam);
        PageResult<SupplierContractDetail> re = new PageResult<>();
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("/pagesDetail")
    public PageResult<SupplierContractDetail> pagesDetail(@RequestBody PageParam<SupplierContractDetail> pageParam) {
        PageResult<SupplierContractDetail> re = new PageResult<>();
        if (null == pageParam.getParam() || null == pageParam.getParam().getSupplierContractId()) {
            re.success(0L, new ArrayList<>());
            return re;
        }
        JsonResult<Page<SupplierContractDetail>> pages = supplierContractDetailService.pages(pageParam);
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody @Valid SupplierContractDetail supplierContractDetail) {
        setInsetOrUpdateUser(supplierContractDetail);

        SupplierContractDetail old = null;
        if (Objects.nonNull(supplierContractDetail.getId())) {
            old = supplierContractDetailService.getById(supplierContractDetail.getId());
        }
        filesService.updateBySaveOrUpdate(supplierContractDetail.getPhoto(), Objects.isNull(old) ? null : old.getPhoto(), supplierContractDetail.getId(), supplierContractDetail.getItemNo());
        supplierContractDetailService.saveOrUpdate(supplierContractDetail);
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/delete/{id}")
    public JsonResult saveOrUpdate(@PathVariable(value = "id") Integer id) {
        supplierContractDetailService.update(new UpdateWrapper<SupplierContractDetail>().lambda()
                .set(SupplierContractDetail::getIsDelete, Const.IS_DELETE_YES)
                .set(SupplierContractDetail::getUpdateUser, UserContextUtil.getUser().getUserName())
                .eq(SupplierContractDetail::getId, id));
        SupplierContractDetail byId = supplierContractDetailService.getById(id);
        filesService.updateByDelete(byId.getPhoto());
        return new JsonResult().success("请求成功");
    }
}
